//
//  ContactServiceViewController.swift
//  weile
//
//  Created by LKM on 16/10/25.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit
import Kingfisher
/**
 * 联系客服
 * Created by lkm on 16-9-12.
 */
class ContactServiceViewController: BaseViewController {

    @IBOutlet weak var tv_phone: UILabel!
    @IBOutlet weak var tv_email: UILabel!
    @IBOutlet weak var tv_qq: UILabel!
    @IBOutlet weak var tv_wx: UILabel!
    
    @IBOutlet weak var img_code: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        loadData();
        
        ViewHelp.setOnClickListening(tv_phone, self, #selector(onTelClick))
        ViewHelp.setOnClickListening(tv_email, self, #selector(onEmailClick))
        ViewHelp.setOnClickListening(tv_qq, self, #selector(onQQClick))
    }
    
    func onTelClick(){
        
        let tels = StringHelp.split(tv_phone.text, "|");
        //LKMLog("\( StringHelp.split(tv_phone.text, "&") )")
        if(CollectionHelp.isEmpty(tels)){
            return
        }
        ViewHelp.showSingleList(self, tips: "拨打", mItems: tels, selectItem: "", onItemClick: { (index:Int) in
            UIApplication.shared.openURL(URL(string:  "tel:\( StringHelp.trim(tels[index]) ?? "" )" )!)
            }, cancleRun: nil)
        
//        if(CollectionHelp.size(tels) == 1 ){
//            UIApplication.shared.openURL(URL(string: "tel:\(tels[0])")!)
//        }else{
//            
//        }
    
    }
    
    func onEmailClick(){
        let emails = StringHelp.split(tv_email.text, "|");
        if(CollectionHelp.isEmpty(emails)){
            return
        }
        ViewHelp.showSingleList(self, tips: "邮件", mItems: emails, selectItem: "", onItemClick: { (index:Int) in
            let m = "mailto://\( StringHelp.trim( emails[index]) ?? "" )";
            
            let openUrl = URL(string:m )
            
            if( openUrl != nil && UIApplication.shared.canOpenURL(openUrl!)){
                
                UIApplication.shared.openURL(openUrl!);
            }else{
                ViewHelp.showTipsView(containView: self.view, tips: "请安装mailto")
            }
            
            }, cancleRun: nil)
    }
    
    func onQQClick(){
//    "501542587"
        
        let qqs = StringHelp.split(tv_qq.text, "|");
        if(CollectionHelp.isEmpty(qqs)){
            return
        }
        ViewHelp.showSingleList(self, tips: "QQ", mItems: qqs, selectItem: "", onItemClick: { (index:Int) in
            let m = "mqq://im/chat?chat_type=wpa&uin=\( StringHelp.trim( qqs[index]) ?? "" )&version=1&src_type=web";
            
            
            let openUrl = URL(string:m )
            
            if( openUrl != nil && UIApplication.shared.canOpenURL(openUrl!)){
                
                UIApplication.shared.openURL(openUrl!);
            }else{
                ViewHelp.showTipsView(containView: self.view, tips: "请安装QQ")
            }
            
            
            
            }, cancleRun: nil)
    }
    
    func onWXClick(){
        let m = "weixin://qr/QeD64j-EGGJnrXuB97VT";
        
        let openUrl = URL(string:m )
        UIApplication.shared.openURL(openUrl!);
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func loadData(){
        ViewHelp.showProgressActivity(containView: self.view);
        let ts=Api.getSysInfo(preTaskSerialNumber: nil) { (re:ResponEntity) in
            ViewHelp.hideProgressActivity(containView: self.view);
//            self.contentScrollView.endRefreshing(at: self.refresher.position);
            guard re.isSuccess() else{
                ViewHelp.showTipsView(containView: self.view, tips: re.msg)//成功不显示
                return;
            }
            let data = re.getData() as! ContactServiceTo;
            
            self.tv_phone.setText(str:data.WebTel);
            self.tv_email.setText(str:data.WebEmail);
            self.tv_qq.setText(str:data.QQ);
            self.tv_wx.setText(str:data.Weixinhao);
            self.img_code.kf.setImage(with: URL(string: Api.getImageUrl(data.getwx_pic() ?? "" ) ), placeholder: Image(named: "PlaceHolderImage_small"), options: nil, progressBlock: nil) { (image, error, cacheType, imageURL) in
                
            }
            
//            ((TextView) findViewById(R.id.tv_phone)).setText(data.WebTel);
//            ((TextView) findViewById(R.id.tv_email)).setText(data.WebEmail);
//            ((TextView) findViewById(tv_qq)).setText(data.QQ);
//            ((TextView) findViewById(R.id.tv_wx)).setText(data.Weixinhao);
//            
//            mImageViewLoadHelp_dp260px.setImage(img_qrcore, Api.getImageUrl(data.getwx_pic()));

            
        }
        taskAlls.append(ts);
    }
    var taskAlls = [Int]();
    deinit {
        //界面退出肯定就不登陆咯
        Api.destroyTask(taskSerialNumbers: taskAlls);
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

import Foundation
import SwiftyJSON

//lkm
class ContactServiceTo: NSObject {
    
    public func getwx_pic() -> String?{
        if (!StringHelp.isEmpty(Dyh_pic)){
            return Dyh_pic;
        }
        return Fwh_pic;
    }
    
    var Appbetter: Int?;//1
    var Weixinhao: String?;//""
    var Dyh: String?;//""
    var Dyh_pic: String?;//"/UpLoadFiles/20160922/2016092212172930.png"
    var Fwh: String?;//""
    var Fwh_pic: String?;//"/UpLoadFiles/20160922/2016092212173157.png"
    var C_name: String?;//"广东唯乐建筑科技有限公司"
    var C_cur: String?;//"唯乐科技"
    var C_Addr: String?;//"广东省广州市天河"
    var C_Addr_2: String?;//"广东省广州市天河"
    var _WebLogo: String?;//"/UpLoadFiles/20160930/2016093015134163.png"
    var AppVersion: String?;//"1.0.1"
    var AppVersion2: String?;//"1.0.1"
    var Webhtml: Int?;//0
    var WebName: String?;//"唯乐建筑"
    var EnglishTitle: String?;//""
    var WebWb163Copyright: String?;//""
    var WebEnglish: Int?;//0
    var WebUrl: String?;//"http://120.76.47.209:1024"
    var WebUrlRewriting: Int?;//1
    var WebPath: String?;//"/"
    var WeblogPath: String?;//"LogFile"
    var WebTel: String?;//"15018796958|18529139695"
    var WebFax: String?;//""
    var WebEmail: String?;//"81675381@qq.com "
    var WebCrod: String?;//"粤ICP备11111号-1"
    var WebCopyright: String?;//"唯乐科技 Copyright © 2016-2063"
    var WebKeywords: String?;//"建筑图纸,效果图,结构图纸,水电图,成本分析,首页图"
    var WebDescription: String?;//"唯乐建筑"
    var WebLogStatus: Int?;//1
    var WebKillKeywords: String?;//"Fuck"
    var WebProSize: Int?;//20
    var WebNewsSize: Int?;//10
    var WebFilePath: String?;//"UpLoadFiles"
    var WebFileType: String?;//"GIF|JPG|PNG|BMP|RAR|DOC|XLS|TXT|PDF"
    var WebFileSize: Int?;//5120
    var IsThumbnail: Int?;//0
    var ProWidth: Int?;//160
    var ProHight: Int?;//160
    var IsWatermark: Int?;//2
    var WatermarkStatus: Int?;//5
    var ImgQuality: Int?;//80
    var ImgWaterPath: String?;//"/UpLoadFiles/20160929/2016092911265503.png"
    var ImgWaterTransparency: Int?;//8
    var WaterText: String?;//"oo"
    var WaterFont: String?;//"Arial"
    var FontSize: Int?;//18
    var Isopen: Int?;//0
    var Ename: String?;//""
    var Epwd: String?;//""
    var Prokey: String?;//"建筑图纸,效果图,结构图纸,水电图,成本分析,首页图"
    var Newkey: String?;//"建筑图纸,效果图,结构图纸,水电图,成本分析,首页图"
    var QQ: String?;//"492019993|81675381"
    
    //dict["data"].dictionaryObject  dict是JSON
    init?(jsons: [String: JSON]?) {
        super.init()
        
        if(jsons == nil){
            return nil
        }
        
        if let dict = jsons{
        
        self.Appbetter = dict["Appbetter"]?.int;//1
        self.Weixinhao = dict["Weixinhao"]?.string;//""
        self.Dyh = dict["Dyh"]?.string;//""
        self.Dyh_pic = dict["Dyh_pic"]?.string;//"/UpLoadFiles/20160922/2016092212172930.png"
        self.Fwh = dict["Fwh"]?.string;//""
        self.Fwh_pic = dict["Fwh_pic"]?.string;//"/UpLoadFiles/20160922/2016092212173157.png"
        self.C_name = dict["C_name"]?.string;//"广东唯乐建筑科技有限公司"
        self.C_cur = dict["C_cur"]?.string;//"唯乐科技"
        self.C_Addr = dict["C_Addr"]?.string;//"广东省广州市天河"
        self.C_Addr_2 = dict["C_Addr_2"]?.string;//"广东省广州市天河"
        self._WebLogo = dict["_WebLogo"]?.string;//"/UpLoadFiles/20160930/2016093015134163.png"
        self.AppVersion = dict["AppVersion"]?.string;//"1.0.1"
        self.AppVersion2 = dict["AppVersion2"]?.string;//"1.0.1"
        self.Webhtml = dict["Webhtml"]?.int;//0
        self.WebName = dict["WebName"]?.string;//"唯乐建筑"
        self.EnglishTitle = dict["EnglishTitle"]?.string;//""
        self.WebWb163Copyright = dict["WebWb163Copyright"]?.string;//""
        self.WebEnglish = dict["WebEnglish"]?.int;//0
        self.WebUrl = dict["WebUrl"]?.string;//"http://120.76.47.209:1024"
        self.WebUrlRewriting = dict["WebUrlRewriting"]?.int;//1
        self.WebPath = dict["WebPath"]?.string;//"/"
        self.WeblogPath = dict["WeblogPath"]?.string;//"LogFile"
        self.WebTel = dict["WebTel"]?.string;//"15018796958|18529139695"
        self.WebFax = dict["WebFax"]?.string;//""
        self.WebEmail = dict["WebEmail"]?.string;//"81675381@qq.com "
        self.WebCrod = dict["WebCrod"]?.string;//"粤ICP备11111号-1"
        self.WebCopyright = dict["WebCopyright"]?.string;//"唯乐科技 Copyright © 2016-2063"
        self.WebKeywords = dict["WebKeywords"]?.string;//"建筑图纸,效果图,结构图纸,水电图,成本分析,首页图"
        self.WebDescription = dict["WebDescription"]?.string;//"唯乐建筑"
        self.WebLogStatus = dict["WebLogStatus"]?.int;//1
        self.WebKillKeywords = dict["WebKillKeywords"]?.string;//"Fuck"
        self.WebProSize = dict["WebProSize"]?.int;//20
        self.WebNewsSize = dict["WebNewsSize"]?.int;//10
        self.WebFilePath = dict["WebFilePath"]?.string;//"UpLoadFiles"
        self.WebFileType = dict["WebFileType"]?.string;//"GIF|JPG|PNG|BMP|RAR|DOC|XLS|TXT|PDF"
        self.WebFileSize = dict["WebFileSize"]?.int;//5120
        self.IsThumbnail = dict["IsThumbnail"]?.int;//0
        self.ProWidth = dict["ProWidth"]?.int;//160
        self.ProHight = dict["ProHight"]?.int;//160
        self.IsWatermark = dict["IsWatermark"]?.int;//2
        self.WatermarkStatus = dict["WatermarkStatus"]?.int;//5
        self.ImgQuality = dict["ImgQuality"]?.int;//80
        self.ImgWaterPath = dict["ImgWaterPath"]?.string;//"/UpLoadFiles/20160929/2016092911265503.png"
        self.ImgWaterTransparency = dict["ImgWaterTransparency"]?.int;//8
        self.WaterText = dict["WaterText"]?.string;//"oo"
        self.WaterFont = dict["WaterFont"]?.string;//"Arial"
        self.FontSize = dict["FontSize"]?.int;//18
        self.Isopen = dict["Isopen"]?.int;//0
        self.Ename = dict["Ename"]?.string;//""
        self.Epwd = dict["Epwd"]?.string;//""
        self.Prokey = dict["Prokey"]?.string;//"建筑图纸,效果图,结构图纸,水电图,成本分析,首页图"
        self.Newkey = dict["Newkey"]?.string;//"建筑图纸,效果图,结构图纸,水电图,成本分析,首页图"
        self.QQ = dict["QQ"]?.string;//"492019993|81675381"
    }
    }
    
    
    //initToList( json:JSON(dict["huxing"] ) )  把JSON过的Any转为JSON
    //一定要是JSON要是 [Any] 的话 会装换失败，最后我还是选用这个JSON
    class func initToList(json: JSON)->[ContactServiceTo?]{
        if let items = json.array {
            var results = [ContactServiceTo?]()
            for item in items {
                let result = ContactServiceTo(jsons: item.dictionary )
                //                if(result != nil){//空还加个屁啊  json为“[]” 时 这是不会为空的。除非直接来了个nil
                //                    results.append(result!)
                //                }
                results.append(result)
            }
            return results;
        }
        return [ContactServiceTo?]()
    }
    
    //initToList( json:JSON(dict["huxing"] ) )  把JSON过的Any转为JSON
    //一定要是JSON要是 [Any] 的话 会装换失败，最后我还是选用这个JSON
    class func initToMap(json: JSON)->Dictionary<String,ContactServiceTo>{
        if let items = json.dictionary {
            var results = Dictionary<String,ContactServiceTo>()//取值的时候本身就是可选类型啊,所以这里不用跟数组一样弄可选类型
            for (key, value)  in items {
                let result = ContactServiceTo(jsons: value.dictionary )
                results[key] = result
            }
            return results;
        }
        return Dictionary<String,ContactServiceTo>()
    }
    

    
    
}
